package com.tw.servlets;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.exception.ConstraintViolationException;

import com.tw.persistence.UserController;


public class SaveAction extends HttpServlet {
	  public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		  System.out.println("Test");
		  String name = request.getParameter("newName");
		  String email = request.getParameter("newEmail");
		  String password = request.getParameter("newPassword");
		  UserController uc = new UserController();
          try {
              uc.save(name,email,password);
          } catch (Exception e) {
              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          }
      }

       public void doGet2(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		  String name = request.getParameter("name");
		  String email = request.getParameter("email");
		  String password = request.getParameter("password");
		  UserController uc = new UserController();

		  String result = "Success";
		  String detail = "";
		  try{ 
			  //uc.save(name,email,password);
			  uc.update(name, email, password);
		  } catch(ConstraintViolationException e){
			  result= "Fail";
			  detail = e.getMessage();
		  } catch(Exception e){
			  result = "Fail";
			  e.printStackTrace();
		  }
		  String destination = "/WEB-INF/jsp/result.jsp";
		  RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
		  request.setAttribute("result", result); 
		  rd.forward(request, response);
	  }
}